package cn.offcn.service.impl;

import cn.offcn.entity.*;
import cn.offcn.mapper.BlogMapper;
import cn.offcn.mapper.EvaluateMapper;
import cn.offcn.mapper.RoleMapper;
import cn.offcn.mapper.UserMapper;
import cn.offcn.service.UserService;
import cn.offcn.utils.OAResult;
import cn.offcn.utils.PageView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author CHTt
 * @Date: 2021/04/22/ 下午 07:12
 * @Description
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageView<User> getUsers(int currentPage,int pageSize,String uname) {

        // 设置分页数据
        PageHelper.startPage(currentPage, pageSize);
        // 查询
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        // 判断uname是否为空
        if (StringUtils.isNoneBlank(uname)){
            criteria.andUnameLike("%"+uname+"%");
        }

        List<User> userList = userMapper.selectByExample(userExample);
        // 创建PageInfo
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        // 创建一个PageView 对象
        PageView<User> pageView = new PageView<User>(currentPage,pageSize);
        pageView.setTotalrecords(pageInfo.getTotal());
        // 设置返回来的数据
        pageView.setRecords(pageInfo.getList());

        return pageView;
    }


    @Override
    public OAResult deleteUserById(int uid) {

        // 先删除评论
        EvaluateExample evaluateExample = new EvaluateExample();
        EvaluateExample.Criteria evaluateExampleCriteria = evaluateExample.createCriteria();
        evaluateExampleCriteria.andUFkEqualTo(uid);
        evaluateMapper.deleteByExample(evaluateExample);
        // 再删除博客
        BlogExample blogExample = new BlogExample();
        BlogExample.Criteria blogExampleCriteria = blogExample.createCriteria();
        blogExampleCriteria.andUFkEqualTo(uid);
        blogMapper.deleteByExample(blogExample);

        //再删用户
        int rows = userMapper.deleteByPrimaryKey(uid);
        if (rows==1){
            return OAResult.ok(200,"删除成功");
        }
        return OAResult.ok(400,"删除失败");
    }

    @Override
    public User getUserById(int uid) {

        User user = userMapper.selectByPrimaryKey(uid);
        Role role = roleMapper.selectByPrimaryKey(user.getUtype());
        user.setRole(role);
        return user;
    }


    @Override
    public OAResult getUserByUname(String username, String password) {

        UserExample userExample = new UserExample();
        UserExample.Criteria userExampleCriteria = userExample.createCriteria();
        userExampleCriteria.andUnameEqualTo(username);

        List<User> userList = userMapper.selectByExample(userExample);
        if (userList!=null && userList.size()>0){
            if (userList.get(0).getUpass().equals(password)){
                return OAResult.build(200,"查询成功",userList.get(0));
            }
        }

        return OAResult.build(400,"用户名或密码错误");
    }
}
